/**
 * Created by IntelliJ IDEA.
 * User: 12
 * Date: 12-5-3
 * Time: 上午9:29
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    $('#add').dialog({
        title:'添加',
        width:380,
        height:260,
        modal:true,
        closed:true,
        //draggable:false,
        buttons:[{
            text:'确定',
            iconCls:'icon-ok',
            handler:okHandler
        },{
            text:'取消',
            handler:function(){
                $('#add').dialog('close');
            }
        }]
    });

    function okHandler(){
        $('#add_form').form('submit',{
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(data) {
                var data = eval('('+data+')');
                if (data.status == '1'){
                    $('#add').dialog('close');      // close the dialog
                    //var node = $('#tt').tree('getSelected');
                    //if (node){
                    //    $('#tt').tree('reload', node.target);
                   // } else {
                        $('#department_tree').treegrid('reload');
                    //}
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: data.msg
                    });
                }
            }

        });
    };


    function lstAdd() {
        var selected = $('#department_tree').treegrid('getSelected');
        $('#add').dialog('open');
        $('#add_form').form("clear");
        $("#parentName").val("根节点") ;
        if (selected){
            $("#parentId").val(selected.id) ;
            $("#parentName").val(selected.text) ;
        }
    }
    function lstUpdate() {
        var selected = $('#department_tree').treegrid('getSelected');

        if (selected){
            $('#add').dialog('open').dialog('setTitle','编辑');
            $('#add_form').form("clear");
            $("#parentId").val(selected.parentId) ;
            $("#parentName").val(selected.parentName) ;
            $("#name").val(selected.text) ;
            $("#description").val(selected.description) ;
            $("#id").val(selected.id) ;
        }
    }
    function lstDelete() {
        var row = $('#department_tree').treegrid('getSelected');
        if (row){
            $.messager.confirm('提示','确定删除该记录?',function(r){
                if (r){
                    $.post($('#deleteUrl').val(),{id:row.id},function(result){
                        if (result.status == '1'){
                            var node = $('#department_tree').treegrid('getSelected');
                            if (node){
                                $('#department_tree').treegrid('remove', node.id);
                            }
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    },'json');
                }
            });
        }
    }

    $(".treegrid-add").click(lstAdd)
    $(".treegrid-edit").click(lstUpdate)
    $(".treegrid-remove").click(lstDelete)


    $('#department_tree').treegrid({
        title:'部门列表',
        nowrap: false,
        rownumbers: true,
        animate:true,
        pagination:true,
        collapsible:true,
        idField:'id',
        treeField:'text',
        frozenColumns:[[
            {title:'名称',field:'text',width:200,
                formatter:function(value){
                    return '<span style="color:red">'+value+'</span>';
                }
            }
        ]],
        toolbar:[{
            id:'btnadd',
            text:'添加',
            iconCls:'icon-add',
            handler:lstAdd
        },{
            id:'btnedit',
            text:'编辑',
            disabled:true,
            iconCls:'icon-edit',
            handler:lstUpdate
        },'-',{
            id:'btnremove',
            text:'删除',
            disabled:true,
            iconCls:'icon-remove',
            handler:lstDelete
        }],
        columns:[[
            {field:'description',title:'备注',width:250}
        ]],
        onBeforeLoad:function(row,param){
            /* if (row){
             $(this).treegrid('options').url = 'treegrid_subdata.json';
             } else {
             $(this).treegrid('options').url = 'treegrid_data.json';
             }*/
        },
        onContextMenu: function(e,row){
            e.preventDefault();
            $(this).treegrid('unselectAll');
            $(this).treegrid('select', row.id);
            $('#mm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        },onClickRow:function(row) {
            $('#btnedit').linkbutton('enable');
            $('#btnremove').linkbutton('enable');
            var desc = $("#client_desc")  ;
            var url = desc.attr("url") ;
            $.get(url,{id:row.id},function(data){
                desc.html(data)
            })
        }
    });

})

